package com.v1.video.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.umeng.analytics.MobclickAgent;
import com.v1.video.Constant;
import com.v1.video.R;
import com.v1.video.activity.PaikeVideoDetailActivity2;
import com.v1.video.activity.ThemeDetailActivity;
import com.v1.video.db.dao.MyThemeTB;
import com.v1.video.db.dao.MyThemeVideoTB;
import com.v1.video.db.dao.ThemeTB;
import com.v1.video.db.service.ThemeMyThemeDB;
import com.v1.video.domain.ScenarioVideoInfo;
import com.v1.video.util.Logger;
import com.v1.video.util.Utils;

import java.util.ArrayList;
import java.util.List;

public class ThemeAllAdapter extends BaseAdapter {
	private final String TAG = "ThemeAllAdapter";
	List<ThemeTB> infos;
	Context context;
	private int mHeight;
	private int mWidth;
	private final int state_refresh = 0x0012;
	public ThemeAllAdapter(Context context, List<ThemeTB> infos) {
		this.infos = infos;
		this.context = context;
		this.mWidth = Utils.computeImageWidthTo3(context);
		this.mHeight = Utils.computeImageHeightWithWidth(mWidth);
		Logger.i(TAG, "mWidth==" + mWidth + "mHeight=" + mHeight);
	}

	public void updata(List<ThemeTB> infos) {
		if (this.infos != null) {
			this.infos.clear();
			this.infos.addAll(infos);
		} else {
			this.infos = infos;
		}
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView != null && convertView instanceof LinearLayout)
			holder = (ViewHolder) convertView.getTag();
		else {
			holder = new ViewHolder();
			convertView = View.inflate(context,
					R.layout.fragment_theme_item_all, null);
			holder.txtGuanZhu = (TextView) convertView
					.findViewById(R.id.item_theme_all_btn_guanzhu);
			holder.title1 = (TextView) convertView
					.findViewById(R.id.item_theme_all_video_title_1);
			holder.title2 = (TextView) convertView
					.findViewById(R.id.item_theme_all_video_title_2);
			holder.title3 = (TextView) convertView
					.findViewById(R.id.item_theme_all_video_title_3);
			holder.imgVideo1 = (ImageView) convertView
					.findViewById(R.id.item_theme_all_video_icon_1);
			holder.imgVideo2 = (ImageView) convertView
					.findViewById(R.id.item_theme_all_video_icon_2);
			holder.imgVideo3 = (ImageView) convertView
					.findViewById(R.id.item_theme_all_video_icon_3);
			holder.titleTheme = (TextView) convertView
					.findViewById(R.id.item_theme_all_title_txt);
			holder.layoutVideoBack = (RelativeLayout) convertView
					.findViewById(R.id.item_theme_all_video_back);
			convertView.setTag(holder);
		}
		final ThemeTB info = infos.get(position);
		android.view.ViewGroup.LayoutParams Params = holder.imgVideo1
				.getLayoutParams();
		Params.width = mWidth; // 设置图片的宽度
		Params.height = mHeight;// 设置图片的高度
		holder.imgVideo1.setLayoutParams(Params);
		android.view.ViewGroup.LayoutParams Params2 = holder.imgVideo2
				.getLayoutParams();
		Params2.width = mWidth; // 设置图片的宽度
		Params2.height = mHeight;// 设置图片的高度
		holder.imgVideo2.setLayoutParams(Params2);
		android.view.ViewGroup.LayoutParams Params3 = holder.imgVideo3
				.getLayoutParams();
		Params3.width = mWidth; // 设置图片的宽度
		Params3.height = mHeight;// 设置图片的高度
		holder.imgVideo3.setLayoutParams(Params3);
		final int pos = position;
		if(info.getVideoList() != null && info.getVideoList().size() > 0){
			if(info.getStatus().equals("0")){
//				holder.txtGuanZhu.setBackgroundResource(R.drawable.icon_attention_bg_selectored);
				holder.txtGuanZhu.setText(context.getResources().getString(R.string.login_user_attention_label));
				holder.txtGuanZhu.setTextColor(context.getResources().getColor(R.color.color_007dff));
				holder.txtGuanZhu.setBackgroundResource(R.drawable.shape_guanzhu_bg);
			}else{
//				holder.txtGuanZhu.setBackgroundResource(R.drawable.icon_attention_bg_selector);
				holder.txtGuanZhu.setText(context.getResources().getString(R.string.login_user_attentioned_label));
				holder.txtGuanZhu.setTextColor(context.getResources().getColor(R.color.color_cccccc));
				holder.txtGuanZhu.setBackgroundResource(R.drawable.shape_yiguanzhu_bg);
			}
			holder.titleTheme.setText(info.getThemeName());
			holder.titleTheme.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					MobclickAgent.onEvent(context, Constant.THEME_ALLTHEMEDETAIL_ID,"全部主题详情-"+info.getThemeID());
					Intent intent = new Intent();
					intent.setClass(context, ThemeDetailActivity.class);
					intent.putExtra("title", info.getThemeName());
					intent.putExtra("themdId", info.getThemeID());
					intent.putExtra("themeImg", info.getThemeImg());
					context.startActivity(intent);
				}
			});
			ImageLoader.getInstance().displayImage(
					info.getVideoList().get(0).getPic(), holder.imgVideo1,
					Constant.VIDEO_10_16_OPTION_ROUNDED);
			holder.title1.setText(info.getVideoList().get(0).getTitle());
			if (info.getVideoList().size() <= 1) {
				holder.imgVideo2.setVisibility(View.GONE);
				holder.title2.setVisibility(View.GONE);
				holder.imgVideo3.setVisibility(View.GONE);
				holder.title3.setVisibility(View.GONE);
			} else if (info.getVideoList().size() <= 2) {
				holder.imgVideo2.setVisibility(View.VISIBLE);
				holder.title2.setVisibility(View.VISIBLE);
				holder.imgVideo3.setVisibility(View.GONE);
				holder.title3.setVisibility(View.GONE);
				ImageLoader.getInstance().displayImage(
						info.getVideoList().get(1).getPic(), holder.imgVideo2,
						Constant.VIDEO_10_16_OPTION_ROUNDED);
				holder.title2.setText(info.getVideoList().get(1).getTitle());
			} else if (info.getVideoList().size() >= 3) {
				holder.imgVideo2.setVisibility(View.VISIBLE);
				holder.title2.setVisibility(View.VISIBLE);
				holder.imgVideo3.setVisibility(View.VISIBLE);
				holder.title3.setVisibility(View.VISIBLE);
				ImageLoader.getInstance().displayImage(
						info.getVideoList().get(1).getPic(), holder.imgVideo2,
						Constant.VIDEO_10_16_OPTION_ROUNDED);
				holder.title2.setText(info.getVideoList().get(1).getTitle());
				ImageLoader.getInstance().displayImage(
						info.getVideoList().get(2).getPic(), holder.imgVideo3,
						Constant.VIDEO_10_16_OPTION_ROUNDED);
				holder.title3.setText(info.getVideoList().get(2).getTitle());

			} else {
				holder.layoutVideoBack.setVisibility(View.GONE);
			}
			
			holder.txtGuanZhu.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					if(info.getStatus().equals("0")){
						info.setStatus("1");
//						Message msg = Message.obtain();
//						msg.what = state_refresh;
//						msg.arg1 = 1;
//						msg.obj = (ViewHolder)v.getTag();
//						handler.sendMessage(msg);
//						ViewHolder holder = (ViewHolder)v.getTag();
//						holder.txtGuanZhu.setBackgroundResource(R.drawable.icon_attention_bg_selectored);
//						holder.txtGuanZhu.setText(context.getResources().getString(R.string.login_user_attentioned_label));
						MyThemeTB theme = new MyThemeTB();
						theme.setThemeID(info.getThemeID());
						theme.setThemeImg(info.getThemeImg());
						theme.setThemeName(info.getThemeName());
						theme.setStatus("1");
						ThemeMyThemeDB.getInstance().insertOrReplaceMyTheme(theme);
						List<MyThemeVideoTB> myThemeVideoList = new ArrayList<MyThemeVideoTB>();
						for(int i = 0; i < info.getVideoList().size();i++){
							MyThemeVideoTB video = new MyThemeVideoTB();
							video.setVid(info.getVideoList().get(i).getVid());
							video.setDuration(info.getVideoList().get(i).getDuration());
							video.setAt(info.getVideoList().get(i).getAt());
							video.setPic(info.getVideoList().get(i).getPic());
							video.setTitle(info.getVideoList().get(i).getTitle());
							video.setThemeID(info.getThemeID());
							myThemeVideoList.add(video);
						}
						ThemeMyThemeDB.getInstance().insertMyThemeVideo(myThemeVideoList);
					}else{
//						Message msg = Message.obtain();
//						msg.what = state_refresh;
//						msg.arg1 = 0;
//						msg.obj = (ViewHolder)v.getTag();
//						handler.sendMessage(msg);						
						info.setStatus("0");
//						ViewHolder holder = (ViewHolder)v.getTag();
//						holder.txtGuanZhu.setBackgroundResource(R.drawable.icon_attention_bg_selector);
//						holder.txtGuanZhu.setText(context.getResources().getString(R.string.login_user_attention_label));
						MyThemeTB myThemeData= ThemeMyThemeDB.getInstance().getSingMyThemeData(info.getThemeID());
						List<MyThemeVideoTB> list = ThemeMyThemeDB.getInstance().getMyThemdVideoFromMyTheme(info.getThemeID());
						if(myThemeData != null){
							ThemeMyThemeDB.getInstance().deleteInTx(list);
							ThemeMyThemeDB.getInstance().delete(myThemeData);
						}
					}
					notifyDataSetChanged();
				}
			});

		}else{
			holder.layoutVideoBack.setVisibility(View.GONE);
		}
		
		holder.imgVideo1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (info.getVideoList() != null && info.getVideoList().get(0) != null) {
					ScenarioVideoInfo video = new ScenarioVideoInfo(info
							.getVideoList().get(0).getVid()
							+ "");
					Intent intent = new Intent();
					if (video != null) {
						intent.setClass(context, PaikeVideoDetailActivity2.class);
						intent.putExtra("videoInfo", video);
						intent.putExtra("source", Constant.ADV_SOURCE_SUBJECT);
						context.startActivity(intent);
					}
				}
			}
		});
		holder.imgVideo2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (info.getVideoList() != null && info.getVideoList().get(1) != null) {
					ScenarioVideoInfo video = new ScenarioVideoInfo(info
							.getVideoList().get(1).getVid()
							+ "");
					Intent intent = new Intent();
					if (video != null) {
						intent.setClass(context, PaikeVideoDetailActivity2.class);
						intent.putExtra("videoInfo", video);
						intent.putExtra("source", Constant.ADV_SOURCE_SUBJECT);
						context.startActivity(intent);
					}
				}
			}
		});
		holder.imgVideo3.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (info.getVideoList() != null && info.getVideoList().get(2) != null) {
					ScenarioVideoInfo video = new ScenarioVideoInfo(info
							.getVideoList().get(2).getVid()
							+ "");
					Intent intent = new Intent();
					if (video != null) {
						intent.setClass(context, PaikeVideoDetailActivity2.class);
						intent.putExtra("videoInfo", video);
						intent.putExtra("source", Constant.ADV_SOURCE_SUBJECT);
						context.startActivity(intent);
					}
				}
			}
		});
		convertView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
//				if (info.getUser() != null) {
//					Intent intent = new Intent();
//					intent.setClass(context, OtherHomeActivity.class);
//					intent.putExtra("userId", info.getUser().getUid());
//					context.startActivity(intent);
//				}
			}
		});
		return convertView;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public Object getItem(int position) {
		return infos.get(position);
	}

	@Override
	public int getCount() {
		return infos.size();
	}

	static class ViewHolder {
		TextView title1;
		TextView title2;
		TextView title3;
		ImageView imgVideo1;
		ImageView imgVideo2;
		ImageView imgVideo3;
		TextView txtGuanZhu;
		TextView titleTheme;
		RelativeLayout layoutVideoBack;
	}
	Handler handler = new Handler(){
		
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			switch (msg.what) {
			case state_refresh:
				ViewHolder holder = (ViewHolder)msg.obj;
				if(msg.arg1 == 0){
//					holder.txtGuanZhu.setBackgroundResource(R.drawable.icon_attention_bg_selector);
					holder.txtGuanZhu.setText(context.getResources().getString(R.string.login_user_attention_label));
					holder.txtGuanZhu.setTextColor(context.getResources().getColor(R.color.color_007dff));
					holder.txtGuanZhu.setBackgroundResource(R.drawable.shape_guanzhu_bg);
				}else{
//					holder.txtGuanZhu.setBackgroundResource(R.drawable.icon_attention_bg_selectored);
//					holder.txtGuanZhu.setText(context.getResources().getString(R.string.login_user_attentioned_label));
					holder.txtGuanZhu.setText(context.getResources().getString(R.string.login_user_attentioned_label));
					holder.txtGuanZhu.setTextColor(context.getResources().getColor(R.color.color_cccccc));
					holder.txtGuanZhu.setBackgroundResource(R.drawable.shape_yiguanzhu_bg);
				}
				break;

			default:
				break;
			}
		}
		
	};
}
